package jschool.net.servidor;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Servidor {
	
	private static Servidor servidor;
	
	private Socket sc = null;
	private static ServerSocket ssc = null;

	private InetAddress ip = null;
	
	private Thread hiloConexion;
	private Conexion conexion;
	
//--------------------------------------------------------------------------------
	
	private Servidor( int puerto ) {
		crearServidor( puerto );
	}
	
//--------------------------------------------------------------------------------
	
	public static Servidor getInstancia( int puerto ){
		
		if( servidor == null ) {
			servidor = new Servidor( puerto );
		}
		
		return servidor;
	}
	
//--------------------------------------------------------------------------------
	
	public void crearServidor( int puerto ) {
		
		try {
			ssc = new ServerSocket( puerto );
			
			while( true ) {
				sc = ssc.accept();
				conexion = new Conexion( sc );
				hiloConexion = new Thread( conexion );
				hiloConexion.start();
			}
		}
		catch( IOException ioe ) {
			ioe.printStackTrace();
		}
		
	}
	
//--------------------------------------------------------------------------------
	
	public ServerSocket getSsc() {	
		return Servidor.ssc;
	}

	public InetAddress getIP() {
		return ip;
	}
}